package io.renren.modules.generator.controller;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import io.renren.modules.generator.entity.ResumeEntity;
import io.renren.modules.generator.service.MyResumeService;
import io.renren.modules.generator.service.ResumeService;
import io.renren.modules.sys.controller.AbstractController;
import io.swagger.models.auth.In;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.generator.entity.InterviewEntity;
import io.renren.modules.generator.service.InterviewService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 面试预约; InnoDB free: 10240 kB
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2019-07-18 14:25:33
 */
@RestController
@RequestMapping("generator/interview")
public class InterviewController extends AbstractController {
    @Autowired
    private InterviewService interviewService;

    @Autowired
    private ResumeService resumeService;

    @Autowired
    private MyResumeService myResumeService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("generator:interview:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = interviewService.queryPage(params);

        return R.ok().put("page", page);
    }


    /*
   预约列表
    */
    @RequestMapping("/getList")
    public R getList(){
        List list = interviewService.getList(getUserId());

        return R.ok().put("list",list);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{intwId}")
    @RequiresPermissions("generator:interview:info")
    public R info(@PathVariable("intwId") Long intwId){
		InterviewEntity interview = interviewService.getById(intwId);

        return R.ok().put("interview", interview);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("generator:interview:save")
    public R save(@RequestBody InterviewEntity interview){
		interviewService.save(interview);

        return R.ok();
    }

    @RequestMapping("/add")
    public R add(Long resId,String intwBookTime){
//        System.out.println(resId+","+intwBookTime);
        try{
            intwBookTime = intwBookTime.replace("Z", " UTC");
            SimpleDateFormat utcFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
            SimpleDateFormat defaultFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            java.util.Date d = utcFormat.parse(intwBookTime);
            intwBookTime=defaultFormat.format(d);
        }catch (Exception e){
            e.printStackTrace();
        }

        ResumeEntity resume=resumeService.getById(resId);
//        System.out.println(resume);
        InterviewEntity interview=new InterviewEntity();
        interview.setIntwHrId(getUserId());
        interview.setIntwResumeId(resId);
        interview.setIntwBookTime(intwBookTime);
        interview.setIntwName(resume.getResName());
        interview.setIntwMobile(resume.getResMobile());
//        System.out.println(intwBookTime+","+resume.getResName()+","+resume.getResMobile());
        interviewService.save(interview);
        myResumeService.updateResState(resId,getUserId(),0L);//修改简历状态为预约
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("generator:interview:update")
    public R update(@RequestBody InterviewEntity interview){
		interviewService.updateById(interview);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("generator:interview:delete")
    public R delete(@RequestBody Long[] intwIds){
		interviewService.removeByIds(Arrays.asList(intwIds));

        return R.ok();
    }

}
